﻿using HtmlAgilityPack;
using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RequestOptions ro = new RequestOptions();
       
           
       

            var news = this.textBox3.Text.Split("\r\n");
            var startid = 100;
            var allsql = "";

            foreach (var url in news)
            {
                ro.Uri = new Uri("http://www.zbjgjsw.gov.cn/" + url);
                ro.Method = "Get";
                string rs = RequestClass.RequestAction(ro);

                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(rs);
                //取得标题

                var title = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='pg-content']/div/div/div/div[3]/div/div/div/div[1]/h1").InnerText.Trim();
                var content  = htmlDoc.DocumentNode.SelectSingleNode("//*[@id='pg-content']/div/div/div/div[3]/div/div/div/div[3]").InnerHtml;
                 

                var sql = $"insert into [main].[ay_content] values({startid}, 'cn', '7', '', '{title}', '#333333', '', '', '超级管理员', '本站', '', '2021-05-14 22:51:55', '', '', '{content}', '', '', '', '', 255, '1', '0', '0', '0', 2, 0, 0, 'admin', 'admin', '2021-05-14 22:52:42', '2021-05-14 22:52:42', '4', '0', '');\r\n";
                allsql += sql;
                startid++;
                content = "";
            }
            this.textBox2.Text = allsql;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var listpages = this.textBox1.Text;
            var allist = listpages.Split("\r\n");
            RequestOptions ro = new RequestOptions();

            //按页抓列表 http://www.zbjgjsw.gov.cn/news.asp?classid=6&page=4
            foreach (var url in allist)
            {
                ro.Uri = new Uri(url);
                ro.Method = "Get";
                string rs = RequestClass.RequestAction(ro);

                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(rs);
                //取得列表

                HtmlNodeCollection newsNodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='items']");
                foreach(var node in newsNodes)
                {
                    var ats=node.SelectSingleNode("a[1]").Attributes;
                    this.textBox3.Text += "\r\n" + ats["href"].Value;
                }

            }


        }
    }
}
